package cn.sky.demo.task;

import cn.sky.demo.mapper.CronMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author sky
 * @date 2025年09月17日 11:42
 * @description
 * @since 1.0.0
 */
//@Component
@EnableScheduling
@Slf4j
public class ScheduleTask0 implements SchedulingConfigurer{
    @Autowired
    private CronMapper cronMapper;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //添加任务内容
                () -> processs("基于接口的定时任务"),
                triggerContext -> {
                    //查询cron表达式
                    String cron = cronMapper.getCron(1L);
                    if (cron.isEmpty()) {
                        processs("cron is null");
                    }
                    // 任务触发，可修改任务执行周期
                    //return java.time.Duration.ofSeconds(5);
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

    private void processs(String msg) {
        log.info(msg+",执行时间："+ LocalDateTime.now());
    }
}
